package com.example.zp;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;

import static android.content.ContentValues.TAG;


public class AddressFragment extends Fragment {
    private TextureMapView mapView = null;
    private BaiduMap baiduMap;
    private boolean isFirstLocate = true;
    private LocationClient mLocationClient;
    private TextView textLat;
    private TextView textLon;
    private TextView postionText;
    public MyLocationListener myListener = new MyLocationListener();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        SDKInitializer.initialize(getActivity().getApplicationContext());
        mLocationClient = new LocationClient(getContext());
        mLocationClient.registerLocationListener(myListener);
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.tab_address, container, false);
        postionText = (TextView) rootView.findViewById(R.id.tv_Add);
        textLat = (TextView) rootView.findViewById(R.id.tv_Lat);
        textLon = (TextView) rootView.findViewById(R.id.tv_Lon);
        mapView = (TextureMapView) rootView.findViewById(R.id.bmapView);
        baiduMap = mapView.getMap();
        baiduMap.setMyLocationEnabled(true);//显示当地位置
        mLocationClient.start();
        initLocation();
        return rootView;
    }

    private void navigateTo(BDLocation location) {
        if (isFirstLocate) {
            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
            baiduMap.animateMapStatus(update);
            update = MapStatusUpdateFactory.zoomTo(16f);//缩放级别
            baiduMap.animateMapStatus(update);//传入到经纬度
            isFirstLocate = false;//防止多次调用
        }
        MyLocationData.Builder locationBuilder = new MyLocationData.Builder();
        locationBuilder.latitude(location.getLatitude());
        locationBuilder.longitude(location.getLongitude());
        MyLocationData locationData = locationBuilder.build();
        baiduMap.setMyLocationData(locationData);
    }

    private void initLocation() {//设置更新时间的间隔
        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        option.setScanSpan(5000);//每隔5000毫秒一次更新
        option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);//强制GPS
        option.setIsNeedAddress(true);//需要精确的信息
        mLocationClient.setLocOption(option);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mapView.onDestroy();
        mLocationClient.stop();
        baiduMap.setMyLocationEnabled(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    //
    class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation.getLocType() == bdLocation.TypeGpsLocation || bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
                navigateTo(bdLocation);
            }
            StringBuilder lat =  new StringBuilder().append(bdLocation.getLatitude());
            StringBuilder lon =  new StringBuilder().append(bdLocation.getLongitude());
            StringBuilder position = new StringBuilder().append(bdLocation.getAddrStr());
            Log.d(TAG, lat.toString());
            Log.d(TAG, lon.toString());
            textLat.setText(lat.toString());
            textLon.setText(lon.toString());
            postionText.setText(position.toString());
        }
    }
}

